﻿using System;
using System.Data;
using System.Data.OleDb;

namespace YArchitech.LIB
{
	public class DataAccessOperate
	{
		public static string ConfigFile
		{
			get
			{
				return DataAccessOperate.m_configFile;
			}
			set
			{
				DataAccessOperate.m_configFile = value;
			}
		}

		public static string DataSource
		{
			get
			{
				return DataAccessOperate.m_DataSource;
			}
		}

		private static string ConnectionString
		{
			get
			{
				return DataAccessOperate.m_ConnectionString;
			}
		}

		private static OleDbConnection Connection
		{
			get
			{
				return DataAccessOperate.m_Connection;
			}
		}

		~DataAccessOperate()
		{
			if (DataAccessOperate.m_Connection != null)
			{
				DataAccessOperate.m_Connection.Close();
			}
		}

		public static void SetDataSource(string dataSource)
		{
			if (dataSource == DataAccessOperate.m_DataSource)
			{
				return;
			}
			DataAccessOperate.m_DataSource = dataSource;
			if (Environment.Is64BitOperatingSystem)
			{
				DataAccessOperate.m_ConnectionString = string.Format("Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Persist Security Info=False", dataSource);
			}
			else
			{
				DataAccessOperate.m_ConnectionString = string.Format("Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0};Persist Security Info=False", dataSource);
			}
			if (DataAccessOperate.m_Connection != null && DataAccessOperate.m_Connection.State != ConnectionState.Closed)
			{
				DataAccessOperate.m_Connection.Close();
			}
			DataAccessOperate.m_Connection = new OleDbConnection(DataAccessOperate.m_ConnectionString);
			try
			{
				DataAccessOperate.m_Connection.Open();
			}
			catch (Exception ex)
			{
				throw ex;
			}
		}

		public static OleDbCommand NewCommand()
		{
			return DataAccessOperate.Connection.CreateCommand();
		}

		public static DataSet GetDataset(string sqlStr)
		{
			DataSet dataSet = new DataSet();
			DataSet result;
			try
			{
				new OleDbDataAdapter(sqlStr, DataAccessOperate.Connection).Fill(dataSet);
				result = dataSet;
			}
			catch (Exception)
			{
				result = null;
			}
			return result;
		}

		public static DataTable GetDataTable(string sqlStr)
		{
			DataSet dataSet = new DataSet();
			DataTable result;
			try
			{
				new OleDbDataAdapter(sqlStr, DataAccessOperate.Connection).Fill(dataSet);
				if (dataSet.Tables.Count > 0)
				{
					result = dataSet.Tables[0];
				}
				else
				{
					result = null;
				}
			}
			catch (Exception)
			{
				result = null;
			}
			return result;
		}

		public static bool Query(string sqlStr)
		{
			OleDbCommand oleDbCommand = new OleDbCommand(sqlStr, DataAccessOperate.Connection);
			bool result;
			try
			{
				oleDbCommand.ExecuteNonQuery();
				result = true;
			}
			catch (Exception)
			{
				result = false;
			}
			return result;
		}

		public static object GetSingle(string sqlStr)
		{
			OleDbCommand oleDbCommand = new OleDbCommand(sqlStr, DataAccessOperate.Connection);
			object result;
			try
			{
				object obj = oleDbCommand.ExecuteScalar();
				if (object.Equals(obj, null) || object.Equals(obj, DBNull.Value))
				{
					result = null;
				}
				else
				{
					result = obj;
				}
			}
			catch (OleDbException ex)
			{
				throw new Exception(ex.Message);
			}
			return result;
		}

		private static string m_configFile = string.Empty;

		private static string m_DataSource = string.Empty;

		private static string m_ConnectionString = string.Empty;

		private static OleDbConnection m_Connection = null;
	}
}
